<?php

namespace App\Utils;

class WxUtil
{
    private $sslCertPath;
    private $sslKeyPath;
    //const SSLCERT_PATH = './cert/apiclient_cert.pem';
    //const SSLKEY_PATH  = './cert/apiclient_key.pem';
    const MCHID        = '1652148160';
    const APPID        = 'wx17b79acb8511f822';
    const KEY          = 'A3A39536506D11B1814EE1F176981C78';

    public function __construct()
    {
        $ds = DIRECTORY_SEPARATOR;
        $publicPath = dirname(app_path()) . $ds . "public";
        $this->sslCertPath = $publicPath . "{$ds}cert{$ds}apiclient_cert.pem";
        $this->sslKeyPath = $publicPath . "{$ds}cert{$ds}apiclient_key.pem";
    }

    /**
     * 付款到微信零钱
     * sOpenid：收款方openid
     * nMoney：转账金额
     */
    public function transfer_batches($sOpenid, $nMoney)
    {
        // 付款到零钱方法url
        $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';

        // 订单号
        $sOrderId = 'test' . time();
        // 转账备注 （微信用户会收到该备注）
        $tRemark = 'test' . $sOrderId;
        // 转账金额：分
        $transfer_amount = $nMoney;

        // 转账接收列表设置
        $transfer_detail_list = array(
            [
                'out_detail_no'   => $sOrderId,                  // 明细单号
                'transfer_amount' => intval($transfer_amount),   // 转账总金额
                'transfer_remark' => $tRemark,                   // 单条转账备注
                'openid'          => $sOpenid,                   // 收款方openid

                // 'user_name'       => '张三',
                // 转账金额 >= 2,000元，收款用户姓名必填
            ],
        );
        // 请求参数设置
        $params = [                                                // 请求参数设置
            'appid'                => self::APPID,                 // 文档顶部定义
            'out_batch_no'         => $sOrderId,                   // 商家批次单号
            'batch_name'           => 'x年x月转账',                 // 转账的名称
            'batch_remark'         => 'x年x月转账',                 // 转账的备注
            'total_amount'         => intval($transfer_amount),    // 转账总金额
            'total_num'            => 1,                           // 转账总笔数
            'transfer_detail_list' => $transfer_detail_list,       // 转账接收列表
        ];
        // 获取token
        $token = $this->getToken($params);
        // 发送请求
        $res = $this->https_request($url, json_encode($params), $token);
        // 反馈数组化
        $resArr = json_decode($res, true);

        dd($resArr);
        // 存储转账成功信息或别的操作

        // 成功实例返回值
        //{
        //    "out_batch_no": "plfk2020042013",                        // wx唯一识别号
        //  "batch_id": "1030000071100999991182020050700019480001",  // 商家批次单号
        //  "create_time": "2015-05-20T13:29:35.120+08:00"           // 创建时间
        //}
    }

    /**
     * 构造请求
     */
    function https_request($url, $data = null, $token)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, (string) $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        // 添加请求头
        $headers = [
            'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $token,
            'Accept: application/json',
            'Content-Type: application/json; charset=utf-8',
            'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
        ];
        if (!empty($headers)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }

        $output = curl_exec($curl);
        curl_close($curl);

        return $output;
    }

    /**
     * 获取token
     */
    public function getToken($pars)
    {
        $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
        $http_method = 'POST';                                  // 请求方法（GET,POST,PUT）
        $timestamp = time();                                  // 请求时间戳
        $url_parts = parse_url($url);                         // 获取请求的绝对URL
        $nonce = $timestamp . rand('10000', '99999');     // 请求随机串
        $body = json_encode((object) $pars);              // 请求报文主体
        $stream_opts = [
            "ssl" => [
                "verify_peer"      => false,
                "verify_peer_name" => false,
            ]
        ];

        // 证书路径信息：文档顶部定义
        $apiclient_cert_path = $this->sslCertPath;
        $apiclient_key_path = $this->sslKeyPath;

        $apiclient_cert_arr = openssl_x509_parse(file_get_contents($apiclient_cert_path, false,
            stream_context_create($stream_opts)));
        // 证书序列号
        $serial_no = $apiclient_cert_arr['serialNumberHex'];
        // 密钥
        $mch_private_key = file_get_contents($apiclient_key_path, false, stream_context_create($stream_opts));
        // 商户id：文档顶部定义
        $merchant_id = self::MCHID;
        $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
        $message = $http_method . "\n" .
                   $canonical_url . "\n" .
                   $timestamp . "\n" .
                   $nonce . "\n" .
                   $body . "\n";
        openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
        // 签名
        $sign = base64_encode($raw_sign);
        //$schema = 'WECHATPAY2-SHA256-RSA2048';
        $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $merchant_id, $nonce,
            $timestamp, $serial_no, $sign);
        // 微信返回token
        return $token;
    }
}
